package com.hnucm.house.ui.fragment;

import android.net.Uri;

import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.RenderersFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.hnucm.house.R;
import com.hnucm.house.base.BaseFragment;
import com.hnucm.house.base.BasePresenter;

import butterknife.BindView;

public class VidieFragment extends BaseFragment {
    private SimpleExoPlayer player;
    @BindView(R.id.play_view)
    SimpleExoPlayerView simpleExoPlayerView;
    @Override
    protected int getContentViewId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void init() {
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory trackFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(trackFactory);
        player = ExoPlayerFactory.newSimpleInstance((RenderersFactory) new DefaultRenderersFactory(getActivity()), trackSelector);
        simpleExoPlayerView.setPlayer(player);
        preparePlay();
    }
    private void preparePlay() {
        DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getActivity()
                , Util.getUserAgent(getActivity(), "exoplayer_sample")
                , defaultBandwidthMeter);
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
        MediaSource mediaSource = new ExtractorMediaSource(uri
                , dataSourceFactory, extractorsFactory, null, null);
        //player.addListener(this);
        player.prepare(mediaSource);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        releasePlayer();
    }
    private void releasePlayer() {
        if (player != null) {
//            playbackPosition = player.getCurrentPosition();
//            currentWindow = player.getCurrentWindowIndex();
//            playWhenReady = player.getPlayWhenReady();
            player.release();
            player = null;
        }
    }


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
